package me.green.util;

import java.util.List;

import me.green.bean.Diary;
import me.green.bean.Plan;

/**
 * @author green
 */
public class HtmlBuilder {

    /**
     * 生成日志的HTML字符串
     *
     * @param list
     * @return
     */
    public static String genDiariesHtmlString(List<Diary> list) {
        StringBuilder html = new StringBuilder("<body>");
        if (list.isEmpty()) {
            html.append("<div class='tip'>开始写日记吧</div>");
            return html.toString();
        }
        int size = list.size();
        html.append("<table>");
        for (int i = 0; i < size; i++) {
            Diary diary = list.get(i);
            int idx = i + 1;
            html.append("<tr>");
            html.append("<td class='idx'>" + idx + "</td>");
            html.append("<td class='time'>" + DateUtil.formatDate2ReadString(diary.getAddDate(), false) + "</td >");
            html.append("<td class='title'><a href='detail/" + diary.getId() + "'>" + diary.getAbstractWork() + "</a></td>");
            html.append("</tr>");
        }
        html.append("</table>");
        html.append("</body>");
        return html.toString();
    }

    /**
     * 得到单条完成计划的详细Html字符串
     *
     * @param diary
     * @return
     */
    public static String genDiaryDetailHtmlString(Diary diary) {
//        diary.setAbstractWork(diary.getAbstractWork().replaceAll("<", "&lt;"));
//        diary.setAbstractWork(diary.getAbstractWork().replaceAll(">", "&gt;"));
//        diary.setMood(diary.getMood().replaceAll("<", "&lt;"));
//        diary.setMood(diary.getMood().replaceAll(">", "&gt;"));
//        diary.setDetail(diary.getDetail().replaceAll("<", "&lt;"));
//        diary.setDetail(diary.getDetail().replaceAll(">", "&gt;"));

        String content = diary.getDetail().replaceAll("\n", "<br>");
        StringBuilder html = new StringBuilder("<body class='c-article'>");
        if (diary == null) {
            html.append("<div class='tip'>没有发现日记！</div>");
            return html.toString();
        }
        html.append("<div class=\"header\">");
        html.append("   <div class=\"b-title\">" + diary.getAbstractWork() + "</div>");
        html.append("   <div class=\"b-time\">" + DateUtil.formatDate2ReadString(diary.getAddDate(), true) + "</div>");
        html.append("</div>");
        html.append("<div class=\"b-content\">" + content + "</div>");
        html.append("</body>");
        return html.toString();
    }

    /**
     * 通过[工作计划]集合得到工作计划的HTML显示字符串
     *
     * @param list
     * @return
     */
    public static String genPlansHtmlString(List<Plan> list) {
        StringBuilder html = new StringBuilder("<body>");
        if (list.size() <= 0) {
            html.append("<div class='tip'>开始添加计划吧</div>");
            return html.toString();
        }
        int size = list.size();
        html.append("<table>");
        for (int i = 0; i < size; i++) {
            Plan plan = list.get(i);
            int idx = i + 1;
            html.append("<tr>");
            html.append("<td class='idx'>" + idx + "</td>");
            html.append("<td class='time'>" + DateUtil.formatDate(plan.getPlanDate()) + "</td >");
            html.append("<td class='plan'>");
            // 归档的计划
            if (plan.getState().equals("2")) {
                html.append("<span class='archive'><strike>" + plan.getDetail() + "</strike></span>");
            } else {
                html.append("<a href='archive/" + plan.getId() + "'>" + plan.getDetail() + "</a>");
            }
            html.append("</td>");
            html.append("<td class='delete'><a href='delete/" + plan.getId() + "'>删除</a></td>");
            html.append("</tr>");
        }
        html.append("</table>");
        html.append("</body>");
        return html.toString();
    }

}
